<?php
/**
 * User
 *
 * PHP version 5
 *
 * @category Controller
 * @version  1.0
 * @author   Duonght <hduongdmit@gmail.com>
 */
App::uses('AppController', 'Controller');
class NewsController extends AppController {

	/**
	* Components
	* @var array
	*/
	public $components = array(
		'Paginator',
		'RequestHandler',
		'ConstAdmin',
		//'Common',
	);
	public $helpers = array('Text', 'Rss');
	public $uses = array('News');

	public function index(){
		if ($this->RequestHandler->isRss() ) {
			$posts = $this->News->find(
					'all',
					array('limit' => 20, 'order' => 'News.last_update DESC')
			);
			$title_for_layout = 'Ngân hàng 24h » Recent posts';
			return $this->set(compact('posts', 'title_for_layout'));
		}

		// this is not an Rss request, so deliver
		// data used by website's interface
		$conditions = array(
				'active' => 1,
				'del_flg' => 0,
		);
		$paginate = array(
				'fields' => '*',
				'limit' => ConstAdminComponent::PAGE_NUMBER_LIMIT,
				'conditions' => $conditions,
				'order' => array(
					'last_update' => 'DESC',
				),
		);

		$this->Paginator->settings = $paginate;
		$list_news = $this->Paginator->paginate('News');

		$page_number_limit = ConstAdminComponent::PAGE_NUMBER_LIMIT;
		$title_for_layout = 'Ngân hàng 24h » Recent posts';
		$this->set(compact('list_news', 'title_for_layout', 'page_number_limit'));
	}

	public function detail($cat_rewrite = null, $new_rewrite = null){
		$options = array();
		$options['fields'] = array(
			'id',
			'name',
			'brief',
			'contents',
			'title_seo',
			'keyword_seo',
			'description_seo',
			'new_picture',
			'create_time',
			'Category.cat_name',
			'Category.cat_rewrite',
		);
		$options['conditions'] = array('new_md5' => md5(md5($new_rewrite) . Configure::read('Security.salt')));
		$options['joins'] = array(
			array(
				'table' => 'categories_multi',
				'alias' => 'Category',
				'type' => 'INNER',
				'conditions' => array(
					'news.category_id = Category.id'
				)
			)
		);

		$detail_news = $this->News->find('all', $options);
		$detail_news = reset($detail_news);
		if ($detail_news == false){
			throw new NotFoundException('Could not find that post');
		}

		$title_for_layout = ($detail_news['News']['title_seo'] ? $detail_news['News']['title_seo'] : $detail_news['News']['name']);
		$keywords_for_layout = $detail_news['News']['keyword_seo'];
		$description_for_layout = $detail_news['News']['description_seo'];

		$url = Router::url(array('controller' => 'categorys', 'action'=>'index' , 'cat_rewrite' => $detail_news['Category']['cat_rewrite']));
		BreadcrumbComponent::set($detail_news['Category']['cat_name'], $url);

		$this->set(
				array(
					'detail_news' => $detail_news,
					'title_for_layout' => $title_for_layout,
					'keywords_for_layout' => $keywords_for_layout,
					'description_for_layout' => $description_for_layout,
					'breadcrumb' => BreadcrumbComponent::create_links(),
				)
		);
	}

	public function view() {

	}
}

